package com.developer.platform.application.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.developer.platform.application.query.assembler.NoticeAssembler;
import com.developer.platform.application.query.dto.NoticeListDto;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.infrastructure.data.mapper.NoticeMapper;
import com.developer.platform.infrastructure.enums.StatusEnum;
import com.veeker.core.bean.Pageable;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2020-11-10
 */
@Component
public class NoticeQueryManager {

    private final NoticeMapper noticeMapper;
    private final NoticeAssembler noticeAssembler;

    public NoticeQueryManager(NoticeMapper noticeMapper,
                              NoticeAssembler noticeAssembler) {
        this.noticeMapper = noticeMapper;
        this.noticeAssembler = noticeAssembler;
    }

    public ResponseModel<NoticeListDto> page(Long groupId, Long templateId,List<Long> userIds,
        StatusEnum status, Pageable pageable){
        return noticeAssembler.toNoticeListDtoPage(
                noticeMapper.selectPage(new Page<>(pageable.getPageNumber(), pageable.getPageSize()),
                    groupId, templateId, Objects.nonNull(status)?status.name():null, userIds));
    }

    public int count(){
        return noticeMapper.selectCount(new LambdaQueryWrapper<>());
    }
}
